#!/usr/bin/env python
# coding=utf-8

import json
from bottle import get, put, post, delete
from common import web_helper, db_helper, convert_helper, json_helper
from common.string_helper import string
from common.except_helper import exception_handling
from logic import device_info_logic, project_info_logic, _common_logic

from api.device import TAEPS

@get('/api/device/device_info/')
@exception_handling
def callback():
    """
    获取列表数据
    """
    # 页面索引
    page_number = convert_helper.to_int1(web_helper.get_query('page', '', is_check_null=False))
    # 页面显示记录数量
    page_size = convert_helper.to_int0(web_helper.get_query('rows', '', is_check_null=False))
    # 排序字段
    sidx = web_helper.get_query('sidx', '', is_check_null=False)
    # 顺序还是倒序排序
    sord = web_helper.get_query('sord', '', is_check_null=False)
    # 类型
    type = web_helper.get_query('type', '类型', is_check_null=False)
    # 设置查询条件
    wheres = []

    devicename = web_helper.get_query('devicename', '设备名称', is_check_null=False)
    if devicename:
        wheres.append('devicename like \'%' + devicename + '%\'')

    devicemac = web_helper.get_query('devicemac', '设备MAC', is_check_null=False)
    if devicemac:
        wheres.append('devicemac = ' + string(devicemac))

    requestcode = web_helper.get_query('requestcode', '申请码', is_check_null=False)
    if requestcode:
        wheres.append('requestcode = ' + string(requestcode))


    # 初始化排序字段
    orderby = None
    ### 设置排序 ###
    if sidx:
        orderby = sidx + ' ' + sord

    # 实例化product_class表操作类product_class_logic
    _device_info_logic = device_info_logic.DeviceInfoLogic()
    result = _device_info_logic.get_list('*,(select projectname from project_info where id=projectid),(select name as productname from product_class where id=devtypeid) ', wheres, page_number, page_size, orderby)
    if result:
        return web_helper.return_raise(json.dumps(result, cls=json_helper.CJsonEncoder))
    else:
        return web_helper.return_msg(-1, "查询失败")



@get('/api/device/device_info/<id:int>/')
@exception_handling
def callback(id):
    """
    获取指定记录
    """
    _device_info_logic = device_info_logic.DeviceInfoLogic()
    # 读取记录
    result = _device_info_logic.get_model_for_pk(id)
    if result:
        # 直接输出json
        return web_helper.return_msg(0, '成功', result)
    else:
        return web_helper.return_msg(-1, "查询失败")



@post('/api/device/device_info/')
@exception_handling
def callback():
    """
    新增记录
    """
    # 检查用户权限
    _common_logic.check_user_power()
    is_import = web_helper.get_query('is_import', '导入标记', is_check_null=False)
    if is_import:
        pass
    else:
        devicename = web_helper.get_form('devicename', '设备名称')
        requestcode = web_helper.get_form('requestcode', '申请码')
        reqperson = web_helper.get_form('reqperson', '申请人', is_check_null=False)
        remarks = web_helper.get_form('remarks', '备注', is_check_null=False)
        projectid = web_helper.get_form('projectid', '项目名称')
        devtypeid = web_helper.get_form('devtypeid', '产品名称')

    # 解析申请码
    reqcode = TAEPS.getmachinecode(requestcode)

    if reqcode.decode(encoding='utf-8') == '-1':
        return web_helper.return_msg(-1, '申请码不合法')
    reqtmp = reqcode.decode(encoding='utf-8')

    cpuseq = reqtmp[0:16]
    devicemac = reqtmp[16:28]
    hardseq = reqtmp[28:40]

    #生成注册码
    regcode = TAEPS.getpollcode(requestcode)
    registcode = regcode.decode(encoding='utf-8')

    # 设置新增参数
    fields = {
        'devicename': string(devicename),
        'devicemac': string(devicemac),
        'requestcode': string(requestcode),
        'registcode': string(registcode),
        'reqperson': string(reqperson),
        'remarks': string(remarks),
        'projectid': string(projectid),
        'devtypeid': string(devtypeid),
        'cpuseq': string(cpuseq),
        'hardseq': string(hardseq)
    }
    # 实例化device_info表操作类device_info_logic
    _device_info_logic = device_info_logic.DeviceInfoLogic()
    # 新增记录
    result = _device_info_logic.add_model(fields)
    # 判断是否提交成功
    if result:
        return web_helper.return_msg(0, '成功')
    else:
        return web_helper.return_msg(-1, "提交失败")



@put('/api/device/device_info/<id:int>/')
@exception_handling
def callback(id):
    """
    修改记录
    """
    # 检查用户权限
    _common_logic.check_user_power()

    devicename = web_helper.get_form('devicename', '设备名称')
    requestcode = web_helper.get_form('requestcode', '申请码')
    reqperson = web_helper.get_form('reqperson', '申请人', is_check_null=False)
    remarks = web_helper.get_form('remarks', '备注', is_check_null=False)
    projectid = web_helper.get_form('projectid', '项目主键')
    devtypeid = web_helper.get_form('devtypeid', '产品主键')

    # 设置新增参数
    fields = {
        'devicename': string(devicename),
        'requestcode': string(requestcode),
        'reqperson': string(reqperson),
        'remarks': string(remarks),
        'projectid': string(projectid),
        'devtypeid': string(devtypeid)
    }


    # 实例化device_info表操作类device_info_logic
    _device_info_logic = device_info_logic.DeviceInfoLogic()
    # 新增记录
    result = _device_info_logic.edit_model(id, fields)
    # 判断是否提交成功
    if result:
        return web_helper.return_msg(0, '成功')
    else:
        return web_helper.return_msg(-1, "提交失败")



@delete('/api/device/device_info/<id:int>/')
@exception_handling
def callback(id):
    """
    删除指定记录
    """
    # 检查用户权限
    _common_logic.check_user_power()

    # 实例化product_class表操作类product_class_logic
    _device_info_logic = device_info_logic.DeviceInfoLogic()
    result = _device_info_logic.delete_model(id)
    # 判断是否提交成功
    if result:
        return web_helper.return_msg(0, '成功')
    else:
        return web_helper.return_msg(-1, "删除失败")

